/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var searchRange = function(nums, target) {
    let s = -1, e = -1;
    let i = -1, j = nums.length;

    if(j===0){
        return [-1,-1];
    }
    if(j===1){
        if(nums[0]===target){
            return [0,0];
        }
        return [-1,-1];
    }

    //search for start
    while(i<j){
        let m = Math.floor((i+j)/2);
        //console.log("s",s,"i",i,"j",j,"m",m);
        if(i === j-1){
            if(nums[m-1]!==target&&nums[m]===target){
                s = m;
                break;
            }else{
                s = -1;
                break;
            }
        }
        if(nums[m-1]!==target&&nums[m]===target){
            s = m;
            break;
        }
        if(nums[m]<target){
            i = m;
        }
        if(nums[m]>=target){
            j = m;
        }
    }

    console.log("s",s,"i",i,"j",j);

    i = s>=0?s-1:-1; j = nums.length;

    //search for end
    while(i<j){
        console.log("e",e,"i",i,"j",j);
        let m = Math.floor((i+j)/2);
        if(i === j-1){
            if(nums[m+1]!==target&&nums[m]===target){
                e = m;
                break;
            }else{
                e = -1;
                break;
            }
        }
        if(nums[m+1]!==target&&nums[m]===target){
            e = m;
            break;
        }
        if(nums[m]<=target){
            i = m;
        }
        if(nums[m]>target){
            j = m;
        }
    }
    //console.log("e",e,"i",i,"j",j);

    return [s,e];
};

console.log(searchRange([0],1));
console.log(searchRange([2,2],1));